package cn.neusoft.service.impl;

import cn.neusoft.domain.ReturnedInfo;
import cn.neusoft.domain.Supplier;
import cn.neusoft.mapper.SupplierMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.SupplierService;
import cn.neusoft.vo.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    public SupplierServiceImpl(SupplierMapper supplierMapper) {
        this.supplierMapper = supplierMapper;
    }

    @Override
    public void add(Supplier supplier) {
        supplierMapper.add(supplier);
    }

    @Override
    public void delete(String id) {
        supplierMapper.delete(id);
    }

    @Override
    public void update(Supplier supplier) {
        supplierMapper.update(supplier);
    }

    @Override
    public List<Supplier> getAll() {
        return supplierMapper.getAll();
    }

    @Override
    public List<Supplier> getByName(String name) {
        return supplierMapper.getByName(name);
    }

    @Override
    public List<Supplier> getSuppliersByPage(int page, int pageSize) {
        int start = (page - 1) * pageSize;
        return supplierMapper.getSuppliersByPage(start, pageSize);
    }

    @Override
    public int getTotalCount() {
        return supplierMapper.getTotalCount();
    }

    @Override
    public PageBean<Supplier> findByPage(BaseQuery query){
        PageBean<Supplier> pageBean = new PageBean<>();
        //开启分页
        PageHelper.startPage(query.getPageNow(),query.getPageSize());
        //分页查询得到当页的集合
        List<Supplier> supplierList = supplierMapper.findByPage(query);
        //创建一个分页的实体PageInfo
        PageInfo<Supplier> pageInfo = new PageInfo<>(supplierList);

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());

        return pageBean;
    }

    @Override
    public PageBean<ReturnedInfo> getReturnToSupplierInfo(BaseQuery query) {
        PageBean<ReturnedInfo> pageBean = new PageBean<>();
        //开启分页
        PageHelper.startPage(query.getPageNow(),query.getPageSize());
        //分页查询得到当页的集合
        List<ReturnedInfo> supplierList = supplierMapper.getReturnToSupplierInfo(query);
        //创建一个分页的实体PageInfo
        PageInfo<ReturnedInfo> pageInfo = new PageInfo<>(supplierList);

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());
        return pageBean;
    }


}
